/**
* @ClassName: CandidateServiceImpl
* @Description: 
* @author YuYang
* @date 2013-6-5 上午12:49:44
*/
package com.demodis.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.demodis.dao.CandidateDaoI;
import com.demodis.dao.UserDaoI;
import com.demodis.dao.impl.CandidateDaoImpl;
import com.demodis.dao.impl.UserDaoImpl;
import com.demodis.domain.TblCandidate;
import com.demodis.domain.TblCredentials;
import com.demodis.domain.TblUser;
import com.demodis.service.CandidateServiceI;
import com.demodis.service.CredentialsServiceI;
import com.demodis.util.Utils;

public class CandidateServiceImpl implements CandidateServiceI {
	
	private CandidateDaoI canDao = new CandidateDaoImpl(); 
	private CredentialsServiceI creDao = new CredentialsServiceImpl();
	private UserDaoI userDao = new UserDaoImpl();

	@Override
	public boolean delete(TblCandidate t) {
		return canDao.delete(t);
	}

	@Override
	public Object get(TblCandidate t) {
		List<TblCandidate> list = canDao.get(t);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("total", canDao.getCount());
		map.put("rows", list);
		return map;
	}

	@Override
	public boolean save(TblCandidate t, String realPath) {
		if(t.getPicture() != null) {
			t.setPictureFileName(Utils.saveFile(t.getPicture(), t.getPictureFileName(), realPath));
		}else {
			t.setPictureFileName("");
		}
		if(t.getInfo() != null) {
			t.setInfoFileName(Utils.saveFile(t.getInfo(), t.getInfoFileName(), realPath));
		}else {
			t.setInfoFileName("");
		}
		
		return canDao.save(t);
	}
	
	@Override
	public TblCandidate getCandidate(Integer canId) {
		return canDao.getCandidate(canId);
	}

	@Override
	public boolean modify(TblCandidate t, String realPath) {
		if(t.getPicture() != null) {
			t.setPictureFileName(Utils.saveFile(t.getPicture(), t.getPictureFileName(), realPath));
		}
		if(t.getInfo() != null) {
			t.setInfoFileName(Utils.saveFile(t.getInfo(), t.getInfoFileName(), realPath));
		}
		
		return canDao.modify(t);
	}
	
	@Override
	public boolean save(TblCandidate t) {
		return false;
	}
	
	@Override
	public boolean modify(TblCandidate t) {
		return false;
	}

	@Override
	public List<TblCandidate> getByCatId(Integer catId, int userId) {
		return canDao.getByCatId(catId, userId);
	}

	@Override
	public TblCandidate getDetailedInfo(Integer canId) {
		TblCandidate can = canDao.getDetailedInfo(canId);
		TblCredentials cre = new TblCredentials();
		cre.setCanId(canId);
		can.setCreList(creDao.get(cre));
		return can;
	}

	@Override
	public boolean doVote(TblCandidate candidate,int userId) {
		String[] canIds = candidate.getIds().split(",");
		for(String canId : canIds) {
			canDao.doVote(Integer.parseInt(canId));
		}
		canDao.saveRecord(userId); 
		return true;
	}

	@Override
	public Object get() {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	public boolean isVote(int userId) {
		return 	canDao.isVote(userId);
	}

	@Override
	public Object queryVoteState(TblCandidate t) {
		List<Map<String, Object>> list = null;
		if(t.getGroupId() == 0) {
			list = canDao.queryVoteState(t);
		}else {
			list = canDao.queryVoteStateByGourpId(t);
		}
		Map<String, Object> map = new HashMap<String, Object>();
		TblUser user = new TblUser();
		user.setQ("");
		user.setGroupId(t.getGroupId());
		map.put("total", userDao.getUserNum(user));
		map.put("rows", list);
		return map;
	}

}
